package com.qixin.api.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.*;
import com.qixin.api.mapper.ComCarMapper;
import com.qixin.api.service.ComCarService;
import com.qixin.api.service.ComFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租车，车辆信息 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-02
 */
@Service
public class ComCarServiceImpl extends ServiceImpl<ComCarMapper, ComCar> implements ComCarService {

    @Autowired
    ComFileService comFileService;
    @Override
    public List<ComCar> findCarByParam(String param) {
        Map map = new HashMap<>();
        if (param != null && !param.equals("")) {
            map = JSONObject.parseObject(param, Map.class);
        }

        return baseMapper.findCarByParam(map);
    }
    @Override
    public List<ComCar> findCarByParama(String param,String carSize,String carKm,String base,String carBrand) {
//        Map map = new HashMap<>();
//        if (param != null && !param.equals("")) {
//            map = JSONObject.parseObject(param, Map.class);
//        }

        return baseMapper.findCarByParama(carSize,carKm,base,carBrand);
    }

    @Override
    public ComCar findCarByCarId(Long carId) {
        return baseMapper.findCarByCarId(carId);
    }

    @Override
    public String insertCar(ComCar comCar, Long[] filesId) throws SQLException {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        this.insert(comCar);
        for (int i = 0; i < filesId.length; i++) {
            ComFile file=new ComFile();
            file.setId(filesId[i]);
            file.setRelationId(comCar.getId());
            if(!comFileService.updateById(file)){
                throw  new SQLException();
            }

        }
        jsonMap.put("flag",true);
        jsonMap.put("msg", "保存成功");
        return JSON.toJSONString(jsonMap);
    }




    //已租出变未租出
    @Override
    public String updatesentsu(Long id) {
        if(baseMapper.updatesentsu(id)>0){
            return  "修改成功";
        }
        return  "修改失败";
    }
    //未租出变已租出
    @Override
    public String updatesentsu2(Long id) {
        if(baseMapper.updatesentsu2(id)>0){
            return  "修改成功";
        }
        return  "修改失败";
    }
    //已租入变未租入
    @Override
    public String updatesentsu3(Long id) {
        if(baseMapper.updatesentsu3(id)>0){
            return  "修改成功";
        }
        return  "修改失败";
    }
    //未租入变已租入
    @Override
    public String updatesentsu4(Long id) {
        if(baseMapper.updatesentsu4(id)>0){
            return  "修改成功";
        }
        return  "修改失败";
    }
    @Override
    public ResultPage<ComCar> selectMyPage(VoPage voPage,HttpSession session) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
		paramMap.put("ptname",((ComPtUser)session.getAttribute("user")).getPtname());
        List<ComCar> list = baseMapper.selectMyPage(paramMap );
        Integer count = baseMapper.countMyPage(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

	@Override
	public ResultPage<ComCar> selectBill(VoPage voPage,HttpSession session) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		paramMap.put("ptname",((ComPtUser)session.getAttribute("user")).getPtname());
		List<ComCar> list = baseMapper.selectBill(paramMap );
		Integer count = baseMapper.countMyPage2(paramMap );
		ResultPage<ComCar>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}

	@Override
    public ResultPage<ComCar> selectMyPage2(VoPage voPage,String carType,HttpSession session,Long id) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id",id);
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("carType", carType);
        paramMap.put("userId", ((ComPtUser)session.getAttribute("user")).getId());
        List<ComCar> list = baseMapper.selectMyPage2(paramMap );
        Integer count = baseMapper.countMyPage5(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    @Override
    public ResultPage<ComCar> search(Map map, VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComCar> list = baseMapper.search(map, paramMap);
        Integer count = baseMapper.countMyPage(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;


    }
    /**
     * 车辆明细搜索
     * @return
     */
    @Override
    public ResultPage<ComCar> searchcardetail(Map map, VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComCar> list = baseMapper.searchcardetail(map, paramMap);
        Integer count = baseMapper.countMyPage(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<ComCar> selectaLL(VoPage voPage,HttpSession session,Long id) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id",id);
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("ptname",((ComPtUser)session.getAttribute("user")).getPtname());
        List<ComCar> list = baseMapper.selectaLL(paramMap);
        Integer count = baseMapper.countMyPage(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    @Override
    public ResultPage<ComCar> selectALL(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComCar> list = baseMapper.selectALL(paramMap);
        Integer count = baseMapper.countMyPage3(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    //批量删除
    @Override
    public boolean deleteAll(ComCar comCar){
        boolean delectall=baseMapper.deleteAll(comCar);
        return delectall;
    }

    @Override
    public ResultPage<ComCar> selectcar(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComCar> list = baseMapper.selectcar(paramMap);
        Integer count = baseMapper.countMyPage4(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public String insertcar(ComCar comCar, Long[] filesId,Long resultURL) throws SQLException {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        this.insert(comCar);
        for (int i = 0; i < filesId.length; i++) {
            ComFile file=new ComFile();
            file.setId(filesId[i]);
            file.setRelationId(comCar.getId());
            if(!comFileService.updateById(file)){
                throw  new SQLException();
            }

        }
        ComFile files=new ComFile();
        files.setId(resultURL);
        files.setRelationId(comCar.getId());
        comFileService.updateById(files);


        jsonMap.put("flag",true);
        jsonMap.put("msg", "保存成功");
        return JSON.toJSONString(jsonMap);
    }



    //平台已租车辆
    @Override
    public ResultPage<ComCar> selecta(VoPage voPage,String carType,HttpSession session,Long id) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id",id);
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
         paramMap.put("carType", carType);
        paramMap.put("userId", ((ComUser)session.getAttribute("user")).getId());
        List<ComCar> list = baseMapper.selecta(paramMap );
        Integer count = baseMapper.counta(paramMap );
        ResultPage<ComCar>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }



}
